#!/usr/bin/env python
#-*- coding:utf-8 -*-

from tweepy import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API

import simplejson

consumer_key = 'GnIpplNa7spzanLpFW4w'
consumer_secret = 'u54jNe9PeJyW9Ki4th4dYYHxsDnNtttgezL1FG46K4'

access_token = '370740476-nWcfsURnH3S7vaMdtPCEijK3iIm6QdCUvabLEcA4'
access_secret = 'bn2YIjXHDuTifr5XPCK8aFcTrYrCNqBUy64RskN64o'

class StdOutListener(StreamListener):
	def on_data(self, data):
		tweet = simplejson.loads(data)
		try:
			if tweet.has_key('retweeted_status'):
				pass
			elif tweet.has_key('delete'):
				print 'delete %s'%tweet['delete']['status']['id']
			else:
				print '@'+tweet['user']['screen_name']
				print ' '+tweet['text']
		except KeyError:
			print data
		return True
	def on_error(self, status):
		print status
	def on_status(self, tweet):
		print tweet

l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = API(auth)

print 'I am %s'%api.me().name

print 'Follows: %s'%(', '.join(map(lambda user: "%s(%s)"%(user.screen_name, user.name), api.friends())))

stream = Stream(auth, l)
#stream.filter(follow=api.friends_ids())
form = []
for i in xrange(ord(u'가'), ord(u'갛') + 1):
	form.append(unichr(i).encode('utf-8'))
print form
stream.filter(track=form)
#stream.sample()
#stream.userstream(async=True)
